using Core.SimpleTemp.Application;
using Core.SimpleTemp.Common.Authorization;
using Core.SimpleTemp.Common.PagingQuery;
using Core.SimpleTemp.Mvc.Api.Internal;
using Core.Toolkit.Authorization;
using Core.Toolkit.Core;
using Core.Toolkit.Core.Api;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Cors;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Threading.Tasks;
using System.Linq;
using System.Collections.Generic;
using Core.SimpleTemp.Repositories.Internal;
using Microsoft.AspNetCore.Http;

namespace Core.SimpleTemp.Api.ZB
{
    /// <summary>
    /// ZB_OfficeSafety
    ///</summary>
    [ApiExplorerSettings(GroupName = "Sys")]//swagger分类标签
    [Route("api/[controller]")]//路由说明
    [EnableCors("any")]//跨域规则
    [ApiController]//APi标识Controller
    [Authorize]//验证是否登录
    public class ZB_OfficeSafetyController : CoreApiController
    {
        private readonly CoreDBContext _coreDBContext;
        private readonly ZB_OfficeSafetyService _service;
        private readonly ZB_SafetyUserService _Safeservice;
        /// <summary>
        /// ZB_OfficeSafety
        ///</summary>
        public ZB_OfficeSafetyController(ZB_OfficeSafetyService service, CoreDBContext coreDBContext)
        {
            _service = service;
            _coreDBContext = coreDBContext;
        }

        #region 基础增删改查
        /// <summary>
        /// 查询
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("GetList1")]
        [PermissionFilter(nameof(ZB_SafetyUserController) + "_" + nameof(ZB_SafetyUserController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
            GetList1(PagingModel<ZB_SafetyUserDto>
                pagingQueryModel)
        {
            IPageModel<ZB_SafetyUserDto>
                result;
            result = await _Safeservice.GetList(pagingQueryModel);

            //List<ZB_OfficeSafetyDto> NewList = new List<ZB_OfficeSafetyDto>();
            //for (int i = 0; i < result.PageData.Count; i++)
            //{
            //   if(NewList.Count==0)
            //    {
            //        NewList.Add(result.PageData[i]);
            //    }
            //   else
            //    {
            //        bool checkid = false;
            //        for (int j = 0; j < NewList.Count; j++)
            //        {
            //            if(NewList[j].Name== result.PageData[i].Name)
            //            {
            //                NewList[j].scope = NewList[j].scope + result.PageData[i].scope;
            //                checkid = true;
            //            }
            //        }
            //        if(checkid==false)
            //        {
            //            NewList.Add(result.PageData[i]);
            //        }
            //    }
            //}
            //for (int i = 0; i < NewList.Count; i++)
            //{
            //    NewList[i].TestingcomentList = result.PageData.Where(x => x.Name == NewList[i].Name).ToList();
            //}

            //result.PageData = NewList;
            //result.RowCount = NewList.Count;



            return this.JsonSuccess(result);
        }

        [HttpPost("GetList")]
        [PermissionFilter(nameof(ZB_SafetyUserController) + "_" + nameof(ZB_SafetyUserController.GetList), AuthorizationConst.LIST)]
        public async Task<IActionResult>
          GetList(PagingModel<ZB_SafetyUserDto>
              pagingQueryModel)
        {
            IPageModel<ZB_SafetyUserDto>
                result;
            result = await _Safeservice.GetList(pagingQueryModel);

            //List<ZB_SafetyUserDto> List = new List<ZB_SafetyUserDto>();
            //for (int i = 0; i < result.PageData.Count; i++)
            //{
            //    List.Add(result.PageData[i]);
            //}

            return this.JsonSuccess(result);
        }




        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("Save")]
        [PermissionFilter(nameof(ZB_OfficeSafetyController) + "_" + nameof(ZB_OfficeSafetyController.SaveAsync), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            SaveAsync(ZB_OfficeSafetyDto dto)
        {
            dto.CreateFactory = UserIdentity.Factory;
            //添加
            if(dto.Name==null)
            {
                dto.Name = dto.Id;
                dto.Id = Guid.NewGuid();
                await _service.InsertAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
            else
            {
                await _service.UpdateAsync(dto);
                return this.JsonSuccess(string.Empty);
            }
        }

        /// <summary>
        /// DeleteMuti
        ///</summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        [HttpPost("DeleteMuti")]
        [PermissionFilter(nameof(ZB_OfficeSafetyController) + "_" + nameof(ZB_OfficeSafetyController.DeleteMutiAsync), AuthorizationConst.DELETE)]
        public async Task<IActionResult>
            DeleteMutiAsync([FromBody]Guid[] ids)
        {
            List<Guid> newList = new List<Guid>();
            for (int i = 0; i < ids.Count(); i++)
            {
                if (_coreDBContext.ZB_OfficeSafety.Where(x => x.Id == ids[i]).Where(b => b.IsDeleted == false).ToList().Count > 0)
                {
                    newList.Add(ids[i]);
                }
            }
            await _service.DeleteBatchAsync(newList.ToArray());
            return this.JsonSuccess();
        }

        /// <summary>
        /// 添加/修改
        ///</summary>
        /// <param name="dto"></param>
        /// <returns></returns>

        [HttpPost("DeleteChild")]
        [PermissionFilter(nameof(ZB_OfficeSafetyController) + "_" + nameof(ZB_OfficeSafetyController.DeleteChild), AuthorizationConst.SAVE)]
        public async Task<IActionResult>
            DeleteChild(Guid id)
        {
            //List<Guid> newList = new List<Guid>();
            //for (int i = 0; i < ids.Count(); i++)
            //{
            //    if (_coreDBContext.ZB_OfficeSafety.Where(x => x.Id == ids[i]).Where(b => b.IsDeleted == false).ToList().Count > 0)
            //    {
            //        newList.Add(ids[i]);
            //    }
            //}
            await _service.DeleteAsync(id);
            return this.JsonSuccess();
        }




        /// <summary>
        /// 根据ID获取信息
        ///</summary>
        /// <param name="id"></param>
        /// <returns></returns>

        [HttpGet("Get")]
        public async Task<IActionResult>
            Get(Guid id)
        {
            var model = await _service.GetAsync(id);
            if (model == null) return this.JsonFaild("未找到对应数据:" + id);

            var retModel = new
            {
                model.Id,
                model.Name,
                model.month,
                model.itemType,
                model.scope,
                model.CreateFactory,
                //model.Dateofaddition,
                //model.Conferencecontent,
                //model.AttachmentId,
                //model.AttachmentName,
            };
            //  附件
            //var attachmentList = await _coreDBContext.SysFile.Where(a => a.B_ID == id).Select(a => "/api/File/DownLoad?id=" + a.Id).ToListAsync();

            return this.JsonSuccess(retModel);
        }

       


        /// <summary>
        /// 导出
        ///</summary>
        /// <param name="pagingQueryModel"></param>
        /// <returns></returns>

        [HttpPost("Export")]
        [PermissionFilter(nameof(ZB_OfficeSafetyController) + "_" + nameof(ZB_OfficeSafetyController.Export), AuthorizationConst.EXPORT)]
        public async Task<IActionResult>
            Export(PagingModel<ZB_OfficeSafetyDto>
                pagingQueryModel)
        {
            var fileName = await _service.Export(pagingQueryModel);
            if (string.IsNullOrWhiteSpace(fileName))
                return this.JsonFaild(NULL_DATA_FOR_EXCEL_EXPORT);
            return this.JsonSuccess(fileName);
        }


        /// <summary>
        /// 导入
        /// </summary>
        /// <param name="data"></param>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost("Import")]
        //[PermissionFilter(nameof(FX_EquipmentFacilitiesController) + "_" + "导入")]
        public async Task<IActionResult> ImportAsync([FromForm]String data, [FromForm]List<IFormFile> file)
        {
            var retMeg = await _service.ImportAsync(file[0], data);
            if (!retMeg.Any())
            {
                return this.JsonSuccess();
            }
            else
            {
                return this.JsonFaild(retMeg[0].ToString());
            }
        }
        #endregion


    }
}
